package cn.csc.module.pay.service.card;

import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.pay.core.client.impl.jh.util.StringUtils;
import cn.csc.module.pay.api.card.dto.PayCardRespDTO;
import cn.csc.module.pay.controller.admin.card.vo.PayCardCreateReqVO;
import cn.csc.module.pay.controller.admin.card.vo.PayCardExportReqVO;
import cn.csc.module.pay.controller.admin.card.vo.PayCardPageReqVO;
import cn.csc.module.pay.controller.admin.card.vo.PayCardUpdateReqVO;
import cn.csc.module.pay.convert.card.PayCardConvert;
import cn.csc.module.pay.dal.dataobject.card.PayCardDO;
import cn.csc.module.pay.dal.mysql.card.PayCardMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.csc.module.pay.enums.ErrorCodeConstants.*;

/**
 * 见证宝资金账户身份认证 Service 实现类
 *
 * @author 华南城
 */
@Service
@Validated
public class PayCardServiceImpl implements PayCardService {

    @Resource
    private PayCardMapper cardMapper;

    @Override
    public Long createCard(PayCardCreateReqVO createReqVO) {
        // 插入
        PayCardDO card = PayCardConvert.INSTANCE.convert(createReqVO);
        cardMapper.insert(card);
        // 返回
        return card.getId();
    }

    @Override
    public void updateCard(PayCardUpdateReqVO updateReqVO) {
        // 校验存在
        validateCardExists(updateReqVO.getId());
        // 更新
        PayCardDO updateObj = PayCardConvert.INSTANCE.convert(updateReqVO);
        cardMapper.updateById(updateObj);
    }

    @Override
    public void deleteCard(Long id) {
        // 校验存在
        validateCardExists(id);
        // 删除
        cardMapper.deleteById(id);
    }

    private void validateCardExists(Long id) {
        if (cardMapper.selectById(id) == null) {
            throw exception(CARD_NOT_EXISTS);
        }
    }

    @Override
    public PayCardDO getCard(Long id) {
        return cardMapper.selectById(id);
    }


    @Override
    public PayCardRespDTO getCardInfo(String memberGlobalId) {
        return cardMapper.getCardInfo(memberGlobalId);
    }

    @Override
    public PayCardRespDTO getValidateCardInfo(String globalId, String companyName, String mobile) {
        PayCardRespDTO dto = cardMapper.getCardInfo(globalId);
        if (dto == null) {
            throw exception(CARD_COMPANY_NOT_MEMBERNUM);
        }
        if (StringUtils.isBlank(dto.getCompanyName()) || !dto.getCompanyName().equals(companyName)) {
            throw exception(CARD_COMPANY_NOT_NAME);
        }
        if (StringUtils.isBlank(dto.getMobile()) || !dto.getMobile().equals(mobile)) {
            throw exception(CARD_COMPANY_NOT_PHONE);
        }
        return dto;
    }

    @Override
    public List<PayCardDO> getCardList(Collection<Long> ids) {
        return cardMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<PayCardDO> getCardPage(PayCardPageReqVO pageReqVO) {
        return cardMapper.selectPage(pageReqVO);
    }

    @Override
    public List<PayCardDO> getCardList(PayCardExportReqVO exportReqVO) {
        return cardMapper.selectList(exportReqVO);
    }

}
